<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/13
 * Time: 15:08
 */

namespace WeiChat\api\dll;

class Curl{
    public function curlGet($url){
        $options = array (
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false
        );

        $ch = curl_init ( $url );
        curl_setopt_array ( $ch, $options );
        $result = curl_exec ( $ch );

        if ($result === false) {
            $result = "cURL Error: " . curl_error ( $ch );
        }else{
            $result = json_decode($result,true);
        }
        curl_close ( $ch );

        return $result;
    }

    public function curlPost($url,$post){
        $options = array (
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $post,
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false
        );

        $ch = curl_init ( $url );
        curl_setopt_array ( $ch, $options );
        $result = curl_exec ( $ch );

        if ($result === false) {
            $result = "cURL Error: " . curl_error ( $ch );
        }else{
            $result = json_decode($result,true);
        }
        curl_close ( $ch );

        return $result;
    }
    public function curlSave($url){
        $options = array (
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_NOBODY => false,
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false
        );

        $ch = curl_init ( $url );
        curl_setopt_array ( $ch, $options );
        $package = curl_exec ( $ch );
        $httpinfo = curl_getinfo( $ch );

        if ($package === false) {
            $package = "cURL Error: " . curl_error ( $ch );
        }
        curl_close ( $ch );
        $fileAll = array_merge(array('header' => $httpinfo),array("body" => $package));

        return $fileAll;
    }
}
